﻿using Acme.BookStore.PolymerizationGoodss;
using Microsoft.AspNetCore.Mvc;
using SC.UserCenter.Infra;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace Acme.BookStore.MerchantManagements
{
    /// <summary>
    ///商户管理接口实现
    /// </summary>
    [ApiExplorerSettings(GroupName = "v2")]
    public class MerchantManagementServices : ApplicationService, IMerchantManagementServices
    {
        /// <summary>
        ///注入
        /// </summary>
        private readonly IRepository<MerchantManagement> repository;
        /// <summary>
        ///构造函数
        /// </summary>
        /// <param name="repository"></param>
        public MerchantManagementServices(IRepository<MerchantManagement> repository)
        {
            this.repository = repository;
        }

        /// <summary>
        ///商户管理删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpDelete("DeleteMerchantManagement")]
        public async Task<ApiResult> DeleteMerchantManagement(Guid Id)
        {
            await repository.DeleteAsync(x => x.Id == Id);
            return ApiResult.Ok();
        }
        /// <summary>
        /// 商户管理显示
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet("GetMerchantManagement")]
        public async Task<ApiResult> GetMerchantManagement(MerchantManagementDTO dto)
        {
            var list = await repository.GetListAsync();
            if (!string.IsNullOrEmpty(dto.MerchantName))
            {
                list = list.Where(x => x.MerchantName.Contains(dto.MerchantName)).ToList();
            }
            return ApiResult.Ok(list);
        }
        /// <summary>
        /// 商户管理添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost("PostMerchantManagement")]
        public async Task<ApiResult> PostMerchantManagement(MerchantManagementDTO dto)
        {
            var list = ObjectMapper.Map<MerchantManagementDTO, MerchantManagement>(dto);
            await repository.InsertAsync(list);
            return ApiResult.Ok(data: list);
        }
        /// <summary>
        ///商户管理修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut("PutMerchantManagement")]
        public async Task<ApiResult> PutMerchantManagement(MerchantManagementupdateDTO dto)
        {
            var list = await repository.FirstOrDefaultAsync(x => x.Id == dto.Id);
            if (list == null)
            {
                return ApiResult.Fail(msg: "商户管理不能为空");
            }

            ObjectMapper.Map(dto, list);
            await repository.UpdateAsync(list);
            return ApiResult.Ok(data: list);
        }
    }
}
